package com.ruoyi.business.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.business.domain.FollowUpRecord;
import com.ruoyi.business.domain.HistoricActivity;
import com.ruoyi.business.service.IFollowUpRecordService;
import com.ruoyi.common.utils.ShiroUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.potentialCustomer;
import com.ruoyi.business.service.IpotentialCustomerService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 潜在客户Controller
 * 
 * @author wolfcode
 * @date 2021-07-26
 */
@Controller
@RequestMapping("/business/potentialCustomer")
public class potentialCustomerController extends BaseController
{
    private String prefix = "business/potentialCustomer";

    @Autowired
    private IpotentialCustomerService potentialCustomerService;
    @Autowired
    private IFollowUpRecordService followUpRecordService;

    @RequiresPermissions("business:potentialCustomer:view")
    @GetMapping()
    public String potentialCustomer()
    {
        return prefix + "/potentialCustomer";
    }

    /**
     * 查询潜在客户列表
     */
    @RequiresPermissions("business:potentialCustomer:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(potentialCustomer potentialCustomer) {
        startPage();
        if (SecurityUtils.getSubject().hasRole("shopOwner")) {
            List<potentialCustomer> list = potentialCustomerService.selectpotentialCustomerList(potentialCustomer);
            return getDataTable(list);
        }else{
            Long userId = ShiroUtils.getUserId();
            potentialCustomer.setFollowUpUser(userId);
            List<potentialCustomer> list = potentialCustomerService.selectpotentialCustomerListById(potentialCustomer);
            return getDataTable(list);
        }

    }

    /**
     * 导出潜在客户列表
     */
    @RequiresPermissions("business:potentialCustomer:export")
    @Log(title = "潜在客户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(potentialCustomer potentialCustomer)
    {
        List<potentialCustomer> list = potentialCustomerService.selectpotentialCustomerList(potentialCustomer);
        ExcelUtil<potentialCustomer> util = new ExcelUtil<potentialCustomer>(potentialCustomer.class);
        return util.exportExcel(list, "潜在客户数据");
    }

    /**
     * 新增潜在客户
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存潜在客户
     */
    @RequiresPermissions("business:potentialCustomer:add")
    @Log(title = "潜在客户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(potentialCustomer potentialCustomer)
    {
        potentialCustomer.setFollowUpUser(ShiroUtils.getUserId());
        return toAjax(potentialCustomerService.insertpotentialCustomer(potentialCustomer));
    }

    /**
     * 修改潜在客户
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        potentialCustomer potentialCustomer = potentialCustomerService.selectpotentialCustomerById(id);
        mmap.put("potentialCustomer", potentialCustomer);
        return prefix + "/edit";
    }

    /**
     * 修改保存潜在客户
     */
    @RequiresPermissions("business:potentialCustomer:edit")
    @Log(title = "潜在客户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(potentialCustomer potentialCustomer)
    {
        return toAjax(potentialCustomerService.updatepotentialCustomer(potentialCustomer));
    }

    /**
     * 删除潜在客户
     */
    @RequiresPermissions("business:potentialCustomer:remove")
    @Log(title = "潜在客户", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(potentialCustomerService.deletepotentialCustomerByIds(ids));
    }

    /**
     * 历史跟进
     * @param id
     * @param mmap
     * @return
     */
    @RequestMapping("/historyPage")
    public String historyPage(long id,ModelMap mmap){
        mmap.put("id",id);
        return prefix +"/historyPage";
    }

    @PostMapping("/listHistory")
    @ResponseBody
    public TableDataInfo listHistory(long id)
    {
        startPage();
        List<FollowUpRecord> list = followUpRecordService.selectFollowUpRecordListById(id);
        return getDataTable(list);
    }
    @RequestMapping("/addGenJin")
    public String addGenJin(long id,ModelMap mmap){
        mmap.put("id",id);
        return prefix +"/genjin";
    }

    @RequiresPermissions("business:potentialCustomer:agenJinSavedd")
    @Log(title = "潜在客户", businessType = BusinessType.INSERT)
    @PostMapping("/genJinSave")
    @ResponseBody
    public AjaxResult genJinSave(FollowUpRecord followUpRecord)
    {
        Date followUpTime = followUpRecord.getFollowUpTime();
        Long customerId = followUpRecord.getCustomerId();
        potentialCustomer potentialCustomer = new potentialCustomer();
        potentialCustomer.setId(customerId);
        potentialCustomer.setLastFollowUpTime(followUpTime);
        potentialCustomerService.updatepotentialCustomer(potentialCustomer);
//        potentialCustomer.setFollowUpUser(ShiroUtils.getUserId());
//        return toAjax(potentialCustomerService.insertpotentialCustomer(potentialCustomer));
        return toAjax(followUpRecordService.insertFollowUpRecord(followUpRecord));
    }
}
